package com.gongj.annon;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.PriorityOrdered;

public class MyBeanDefinitionRegistryPostProcessor2 implements BeanDefinitionRegistryPostProcessor, PriorityOrdered {
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("执行 BeanDefinitionRegistryPostProcessor2 的 postProcessBeanFactory");

		String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
		System.out.println("当前容器中所有 beanDefinitionName = " );
		for (String beanDefinitionName : beanDefinitionNames) {
			System.out.println(beanDefinitionName);
		}
	}

	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

		System.out.println("执行 BeanDefinitionRegistryPostProcessor2 的 postProcessBeanDefinitionRegistry");
		MutablePropertyValues mpvs = new MutablePropertyValues();
		mpvs.add("name","cxyxj");
		RootBeanDefinition rootBeanDefinition =new RootBeanDefinition(Test2.class,null,mpvs);
		//注册 BeanDefinition
		//如果对应的 Bean 已经注册且在配置中配置了 bean 不允许被覆盖，
		//则抛出异常,默认是 true，也就是允许覆盖
		registry.registerBeanDefinition("test2",rootBeanDefinition);
	}

	@Override
	public int getOrder() {
		return 555;
	}
}
